package com.czy.owner.ui.order;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;


import com.czy.owner.R;
import com.czy.owner.adapter.EmployeesAdapter;
import com.czy.owner.api.ServiceTechnicianApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.EmployessModel;
import com.czy.owner.entity.ScopeBusinessModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.GsonUtils;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2016/6/17.
 */
public class ServiceTechnicianActivity extends BaseActivity {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView recyclerView;
    private EmployeesAdapter mAdapter;
    private List<EmployessModel> list;
    private long serviceTime;
    private String employeesIds = "";
    private String employeesNames = "";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_technichian_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.order_service_jishi);
        getSubTitle().setText("确定");
        serviceTime = getIntent().getLongExtra("serviceTime", 0);
        employeesIds = getIntent().getStringExtra("employeesIds");
        employeesNames = getIntent().getStringExtra("employeesNames");
        recyclerView.setLayoutManager(new LinearLayoutManager(ServiceTechnicianActivity.this));
        recyclerView.setAdapterWithProgress(mAdapter = new EmployeesAdapter(ServiceTechnicianActivity.this, employeesIds, employeesNames));
        recyclerView.addItemDecoration(new RecycleViewDivider(ServiceTechnicianActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_order_divider_decoration));
        recyclerView.setEmptyView(R.layout.view_empty_common);
        mAdapter.setSelectListener(new EmployeesAdapter.SelectListener() {
            @Override
            public void onSelectChange(String names, String ids) {
                employeesIds = ids;
                employeesNames = names;
            }
        });
        getDataList();
    }

    @Override
    protected void updateViews() {

    }


    public void getDataList(){
        ServiceTechnicianApi serviceTechnicianApi = new ServiceTechnicianApi(new HttpOnNextListener<List<EmployessModel>>() {
            @Override
            public void onNext(List<EmployessModel> list) {
                setDataList(list);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<EmployessModel>>>() {
                }.getType();
                BaseResultEntity<List<EmployessModel>> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        },this);
        serviceTechnicianApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        serviceTechnicianApi.setStoreId(SharedPreferencesUtils.getParam(ServiceTechnicianActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        serviceTechnicianApi.setServiceTime(serviceTime + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(serviceTechnicianApi);
    }

private void setDataList(List<EmployessModel> list){
    mAdapter.addAll(list);
}



    @OnClick(R.id.toolbar_subtitle)
    public void sure(View view) {
//        if (employeesNames.isEmpty()) {
//            employeesNames="";
//            employeesIds="";
//            Intent intent = new Intent();
//            intent.putExtra("employeesIds", employeesIds);
//            intent.putExtra("name", employeesNames);
//            setResult(RESULT_OK, intent);
//            PhoneUtils.ShowToastMessage(ServiceTechnicianActivity.this, "至少选择一个技师!");
//        } else {
        Intent intent = new Intent();
        intent.putExtra("employeesIds", employeesIds);
        intent.putExtra("name", employeesNames);
        setResult(RESULT_OK, intent);
        finish();
//        }
    }

}
